{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Calculate Money in Leetcode Bank"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: totalMoney"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #计算力扣银行的钱"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>Hercy 想要为购买第一辆车存钱。他 <strong>每天</strong> 都往力扣银行里存钱。</p>\n",
    "\n",
    "<p>最开始，他在周一的时候存入 <code>1</code> 块钱。从周二到周日，他每天都比前一天多存入 <code>1</code> 块钱。在接下来每一个周一，他都会比 <strong>前一个周一</strong> 多存入 <code>1</code> 块钱。<span style=\"\"> </span></p>\n",
    "\n",
    "<p>给你 <code>n</code> ，请你返回在第 <code>n</code> 天结束的时候他在力扣银行总共存了多少块钱。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>n = 4\n",
    "<b>输出：</b>10\n",
    "<b>解释：</b>第 4 天后，总额为 1 + 2 + 3 + 4 = 10 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>n = 10\n",
    "<b>输出：</b>37\n",
    "<b>解释：</b>第 10 天后，总额为 (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37 。注意到第二个星期一，Hercy 存入 2 块钱。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>n = 20\n",
    "<b>输出：</b>96\n",
    "<b>解释：</b>第 20 天后，总额为 (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7 + 8) + (3 + 4 + 5 + 6 + 7 + 8) = 96 。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [calculate-money-in-leetcode-bank](https://leetcode.cn/problems/calculate-money-in-leetcode-bank/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [calculate-money-in-leetcode-bank](https://leetcode.cn/problems/calculate-money-in-leetcode-bank/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['4', '10', '20']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        L=[0]*n\n",
    "        L[0]=1\n",
    "        for i in range(1,n):\n",
    "            L[i]=L[i-1]+1\n",
    "            if i>=7:\n",
    "                L[i]=L[i-7]+1\n",
    "        return sum(L)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        return sum((d + v) if v!=0 else 6+d for d, v in (divmod(num, 7) for num in list(range(1, n+1))))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        # 所有完整的周存的钱\n",
    "        weekNumber = n // 7\n",
    "        firstWeekMoney = (1 + 7) * 7 // 2\n",
    "        lastWeekMoney = firstWeekMoney + 7 * (weekNumber - 1)\n",
    "        weekMoney = (firstWeekMoney + lastWeekMoney) * weekNumber // 2\n",
    "        # 剩下的不能构成一个完整的周的天数里存的钱\n",
    "        dayNumber = n % 7\n",
    "        firstDayMoney = 1 + weekNumber\n",
    "        lastDayMoney = firstDayMoney + dayNumber - 1\n",
    "        dayMoney = (firstDayMoney + lastDayMoney) * dayNumber // 2\n",
    "        return weekMoney + dayMoney\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        sums=0\n",
    "        for t,i in enumerate(range(1,n+1)):\n",
    "            i-=t//7*6\n",
    "            sums+=i\n",
    "        return sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        x=[]\n",
    "        for i in range(1,n+1,7):\n",
    "            j=i//7+1\n",
    "            for k in range(j,j+7,1):\n",
    "                x.append(k)                    \n",
    "        return sum(x[:n])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        k = n // 7 if n % 7 == 0 else n // 7 + 1\n",
    "        for i in range(1, k + 1):\n",
    "            if n > 7:\n",
    "                for j in range(i, i+7):\n",
    "                    ans += j \n",
    "                n -= 7\n",
    "        if n:\n",
    "            while n:\n",
    "                ans += k \n",
    "                k += 1\n",
    "                n -= 1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        a = n // 7\n",
    "        b = n % 7\n",
    "        m = 0\n",
    "        for i in range(a):\n",
    "            m += i + 4\n",
    "        s = 7 * m + (b * (b + 1) // 2) + (b * a)\n",
    "        return s\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        if n//7 == 0:\n",
    "            res = (((n%7)+1)/2)*(n%7)\n",
    "        else:\n",
    "            res = (n//7)*28 + ((n//7-1)*(n//7)/2)*7 + (((n%7)+1)/2)*(n%7) + (n//7)*(n%7)\n",
    "        return int(res)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        return (28+n%7)*(n//7)+(n//7-1)*7*(n//7)//2+(1+n%7)*(n%7)//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        sums=0\n",
    "        for t,i in enumerate(range(1,n+1)):\n",
    "            i-=t//7*6\n",
    "            sums+=i\n",
    "        return sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        w,d = n // 7,n % 7\n",
    "        n1 = w * sum([x for x in range(1,8)]) + sum([x for x in range(w)]) * 7\n",
    "        n2 = sum(x+w+1 for x in range(n % 7))\n",
    "        return n1 + n2    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        cur = 1\n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            if i > 0 and i % 7 == 0:\n",
    "                cur += 1 \n",
    "            ans += i%7+cur\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        start = 1\n",
    "        week = n // 7 + 1\n",
    "        day = n % 7\n",
    "        sum = 0\n",
    "        while start < week:\n",
    "            for i in range(7):\n",
    "                sum += (start+i)\n",
    "            start += 1\n",
    "        \n",
    "        for i in range(day):\n",
    "            print(sum)\n",
    "            sum += (week + i)\n",
    "\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        week = n//7\n",
    "        day = n%7\n",
    "        s = 0\n",
    "        # if week != 0:\n",
    "        #     for i in range(1, week+1):\n",
    "        #         s += (i+3)*7\n",
    "        #     s += (2*week+1+day)*day//2\n",
    "        # else:\n",
    "        #     s += (1+day)*day//2\n",
    "        for i in range(1, week+1):\n",
    "            s += (i+3)*7\n",
    "        s += (2*week+1+day)*day//2\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        m = 0\n",
    "        c = 0\n",
    "        l = [7, 6, 5, 4, 3, 2, 1]\n",
    "\n",
    "\n",
    "        while m < n:\n",
    "            c += 1\n",
    "            m += 7\n",
    "        \n",
    "        return 28 * c + (c * (c - 1)) * 7 // 2 - sum(l[: m - n]) - (c - 1) * (m - n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        sums=0\n",
    "        for t,i in enumerate(range(1,n+1)):\n",
    "            i-=t//7*6\n",
    "            sums+=i\n",
    "        return sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        ####数学\n",
    "        ####计算有几个整周\n",
    "        tmp = (n // 7)\n",
    "        ####不足一周的天数\n",
    "        bit = (n % 7)\n",
    "        return (0 + tmp - 1) * tmp // 2 * 7 + tmp * 28 + (1 + tmp + tmp + bit) * bit // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        b = 1\n",
    "        res = 0\n",
    "        for i in range(1, n + 1):\n",
    "            res += b\n",
    "            b += 1\n",
    "            if i % 7 == 0:\n",
    "                b = i // 7 + 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        n, k = n % 7, n // 7\n",
    "        return (k*28+k*(k-1)*7//2) + ((k+1+k+1+n-1)*n//2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "      sum=0\n",
    "      week,day=0,1\n",
    "      for i in range(1,n+1):       \n",
    "            sum=sum+week+day\n",
    "            day=day+1\n",
    "            if day==8:\n",
    "                  day=1\n",
    "                  week=week+1\n",
    "      return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        i=1\n",
    "        d=1\n",
    "        c=1\n",
    "        ans=0\n",
    "        while d<=n:\n",
    "            ans+=c\n",
    "\n",
    "            d+=1\n",
    "            if i%7==0:\n",
    "                i=1\n",
    "                c=d//7+1\n",
    "            else:\n",
    "                i+=1\n",
    "                c+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        return n//7*28 + sum(i*7 for i in range(n//7)) + sum(i+n//7 for i in range(1, n%7+1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def totalMoney(self, n: int) -> int:\r\n",
    "        sum = 0\r\n",
    "        day1 = 1\r\n",
    "        for week in range(0,n,7):\r\n",
    "            if week + 6 < n:\r\n",
    "                sum += day1*7 + 21\r\n",
    "            else:\r\n",
    "                days = n - week\r\n",
    "                sum += day1 * days + (days-1)*days//2\r\n",
    "            day1 += 1\r\n",
    "        return sum\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        start = 1\n",
    "        week = n // 7 + 1\n",
    "        day = n % 7\n",
    "        sum = 0\n",
    "        while start < week:\n",
    "            for i in range(7):\n",
    "                sum += (start+i)\n",
    "            start += 1\n",
    "        \n",
    "        for i in range(day):\n",
    "            sum += (week + i)\n",
    "\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        s=0\n",
    "        for i in range(1,n+1):\n",
    "            s += (i-1)//7 + (i-1)%7+1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        week=day=1\n",
    "        res=0\n",
    "        for i in range(n):\n",
    "            res+=week+day-1\n",
    "            day+=1\n",
    "            if day==8:\n",
    "                day=1\n",
    "                week+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        w, r = divmod(n, 7)\n",
    "        full = w * (w + 7) * 7 >> 1\n",
    "        full = max(full, 0)\n",
    "        last = r * w + r * (r + 1) // 2\n",
    "        return full + last"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        return int((28+28+7*(n//7-1))*(n//7)/2+(n//7+1+n%7+n//7)*(n%7)//2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        # 1 2 3 4 5 6 7\n",
    "        # 2 3 4 5 6 7 8\n",
    "        weeks = n // 7\n",
    "        a1 = (1 + 7)*7//2\n",
    "        an = a1 + (weeks - 1)*7\n",
    "        s1 = (a1 + an)*weeks//2\n",
    "\n",
    "        days = n % 7\n",
    "        a1 = 1 + weeks\n",
    "        an = a1 + days - 1\n",
    "        s2 = (a1 + an)*days//2\n",
    "        return s1 + s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        ans=0\n",
    "        start,day=1,1\n",
    "        for i in range(n):\n",
    "            ans+=start+day-1\n",
    "            day+=1\n",
    "            if day==8:\n",
    "                day=1\n",
    "                start+=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        m = 0\n",
    "        c = 0\n",
    "        l = [7, 6, 5, 4, 3, 2, 1]\n",
    "\n",
    "\n",
    "        while m < n:\n",
    "            c += 1\n",
    "            m += 7\n",
    "        \n",
    "        return 28 * c + (c * (c - 1)) * 7 // 2 - sum(l[: m - n]) - (c - 1) * (m - n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        return n//7*28+(n%7)*(n%7+1)//2+7*(n//7)*((n//7)-1)//2+(n//7)*(n%7)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        round = n // 7\n",
    "        result = (28 + 28 + 7 * (round - 1)) * round // 2\n",
    "        result += ((round + 1) * 2 + (n - 1) % 7) * (n % 7) // 2\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        li=[1,3,6,10,15,21,28]\n",
    "        return int(28*((n-1)//7)+7*((n-1)//7-1)*((n-1)//7)/2+li[n-1-7*((n-1)//7)]+(n-7*((n-1)//7))*((n-1)//7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        sums=0\n",
    "        for t,i in enumerate(range(1,n+1)):\n",
    "            i-=t//7*6\n",
    "            sums+=i\n",
    "        return sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        w , t = n // 7 , n % 7\n",
    "        if w==0 :\n",
    "            ans = (t+1)*t//2\n",
    "        else:\n",
    "            ans = 28*w + (w-1)*w*7//2 + (2*w+t+1)*t//2\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        if n<=7:\n",
    "            return ((n+1)*n)//2\n",
    "        if n>7:\n",
    "            a=n%7  #余数\n",
    "            b=(n-a)//7 #有多少周\n",
    "            c=((b-1)*7*b)//2+28*b  #前n周\n",
    "            d=a*b+((1+a)*a)//2  #后几天· \n",
    "            res=c+d\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        li=[1,3,6,10,15,21,28]\n",
    "        return int(28*((n-1)//7)+7*((n-1)//7-1)*((n-1)//7)/2+li[n-1-7*((n-1)//7)]+(n-7*((n-1)//7))*((n-1)//7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        f = n//7\n",
    "        k = n%7\n",
    "        ans = 0\n",
    "        ans += 28*f+7*(f-1)*f/2+f*k+k*(k+1)/2\n",
    "        return int(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        return n//7*28 + sum(i*7 for i in range(n//7)) + sum(i+n//7 for i in range(1, n%7+1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        total = 0\n",
    "        loop = n // 7\n",
    "        extra = n % 7\n",
    "        for i in range(loop):\n",
    "            for j in range(i+1,8+i):\n",
    "                total += j\n",
    "        for i in range(loop+1, extra+loop+1):\n",
    "            total += i\n",
    "        \n",
    "        return total\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        week,day = 1,1\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res += week+day-1\n",
    "            day+=1\n",
    "            if day==8:\n",
    "                day=1\n",
    "                week+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        m = 0\n",
    "        c = 0\n",
    "        l = [7, 6, 5, 4, 3, 2, 1]\n",
    "\n",
    "\n",
    "        while m < n:\n",
    "            c += 1\n",
    "            m += 7\n",
    "        \n",
    "        return 28 * c + (c * (c - 1)) * 7 // 2 - sum([i + (c - 1) for i in l[: m - n]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        a = n // 7\n",
    "        b = n % 7\n",
    "        return (a * a + a * 7) // 2 * 7 + (b * (b + 1) // 2) + (b * a)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        week,day = 1,1\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res += week+day-1\n",
    "            day += 1\n",
    "            if day==8:\n",
    "                day = 1\n",
    "                week += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        return sum((d + v) if v!=0 else 6+d for d, v in [divmod(num, 7) for num in list(range(1, n+1))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        m = 0\n",
    "        c = 0\n",
    "        l = [7, 6, 5, 4, 3, 2, 1]\n",
    "\n",
    "\n",
    "        while m < n:\n",
    "            c += 1\n",
    "            m += 7\n",
    "        \n",
    "        return 28 * c + (c * (c - 1)) * 7 // 2 - sum([i + (c - 1) for i in l[: m - n]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        li=[1,3,6,10,15,21,28]\n",
    "        return int(28*((n-1)//7)+7*((n-1)//7-1)*((n-1)//7)/2+li[n-1-7*((n-1)//7)]+(n-7*((n-1)//7))*((n-1)//7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        # 所有完整的周存的钱\n",
    "        weekNumber = n // 7\n",
    "        firstWeekMoney = (1 + 7) * 7 // 2\n",
    "        lastWeekMoney = firstWeekMoney + 7 * (weekNumber - 1)\n",
    "        weekMoney = (firstWeekMoney + lastWeekMoney) * weekNumber // 2\n",
    "        # 剩下的不能构成一个完整的周的天数里存的钱\n",
    "        dayNumber = n % 7\n",
    "        firstDayMoney = 1 + weekNumber\n",
    "        lastDayMoney = firstDayMoney + dayNumber - 1\n",
    "        dayMoney = (firstDayMoney + lastDayMoney) * dayNumber // 2\n",
    "        return weekMoney + dayMoney\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        res = 0\n",
    "        x = 0\n",
    "        for i in range(n):\n",
    "            if i % 7 == 0:\n",
    "                x += 1\n",
    "            res += x + i % 7\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        m = 0\n",
    "        c = 0\n",
    "        # l = [7, 6, 5, 4, 3, 2, 1]\n",
    "        l = [1,2,3,4,5,6,7]\n",
    "\n",
    "\n",
    "        while m + 7 < n:\n",
    "            c += 1\n",
    "            m += 7\n",
    "        \n",
    "        return 28 * c + (c * (c - 1)) * 7 // 2 + sum([i + c for i in l[: n - m]]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        if n <= 7:\n",
    "            return ((n + 1) * n) // 2\n",
    "        if n > 7:\n",
    "            a = n % 7  # 余数\n",
    "            b = (n - a) // 7  # 有多少周\n",
    "            c = ((b - 1) * 7 * b) // 2 + 28 * b  # 前n周\n",
    "            d = a * b + ((1 + a) * a) // 2  # 后几天\n",
    "            res = c + d\n",
    "            return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        a = n // 7\n",
    "        b = n % 7\n",
    "        if a == 0:\n",
    "            return (1+b)*b//2\n",
    "        else:\n",
    "            return (28 * a + (a-1)*a*7//2 + a*b + (1+b)*b//2)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        m = 0\n",
    "        c = 0\n",
    "        l = [7, 6, 5, 4, 3, 2, 1]\n",
    "\n",
    "\n",
    "        while m < n:\n",
    "            c += 1\n",
    "            m += 7\n",
    "        \n",
    "        return 28 * c + (c * (c - 1)) * 7 // 2 - sum([i + (c - 1) for i in l[: m - n]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        return int((28+28+7*(n//7-1))*(n//7)/2+(n//7+1+n%7+n//7)*(n%7)//2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        n1=n//7\n",
    "        n2=n%7\n",
    "        first_week=[i for i in range(1,8)]\n",
    "        ans=sum(first_week)\n",
    "        ans_n=sum(first_week[:n2])\n",
    "        if n1>0:\n",
    "            if n2==0:\n",
    "                result=ans*n1+sum([i for i in range(1,n1)]) * 7\n",
    "            else:\n",
    "                result = ans * n1 + ans_n + n1 * n2 +sum([i for i in range(1,n1)]) * 7 \n",
    "        else:\n",
    "            result=ans * n1 + ans_n + n1 * n2+sum([i for i in range(1,n1)]) * 7\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        ans,cur=0,1\n",
    "        count=n//7\n",
    "        mod=n%7\n",
    "        for i in range(count):\n",
    "            ans+=(cur*2+6)*7//2\n",
    "            cur+=1\n",
    "        for i in range(mod):\n",
    "            ans+=cur\n",
    "            cur+=1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        week, day = 1, 1\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res += week + day - 1\n",
    "            day += 1\n",
    "            if day == 8:\n",
    "                day = 1\n",
    "                week += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        week = n // 7\n",
    "        return (28 * week + 7 * (week - 1) * week // 2) + int(((1 + week) + ((n - week * 7) - 1) / 2) * (n - week * 7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        if n <= 7:\n",
    "            return n*(n+1) >> 1\n",
    "        x = n // 7\n",
    "        y = n % 7     \n",
    "        a = 28*x + (7*x*(x-1) >>1)\n",
    "        b = x*y + (y*(y+1)//2)\n",
    "        return a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        arr = [0] * n\n",
    "        for i in range(1, n+1):\n",
    "            if i <= 7:\n",
    "                arr[i-1] = i\n",
    "            else:\n",
    "                arr[i - 1] = arr[i-8] + 1\n",
    "        return sum(arr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        m = 0\n",
    "        c = 0\n",
    "        l = [7, 6, 5, 4, 3, 2, 1]\n",
    "\n",
    "\n",
    "        while m < n:\n",
    "            c += 1\n",
    "            m += 7\n",
    "        \n",
    "        return 28 * c + (c * (c - 1)) * 7 // 2 - sum(l[: m - n]) - (c - 1) * (m - n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        s = n // 7 \n",
    "        t = n % 7\n",
    "        return (s*(s+7)//2)*7+(s+1+s+t)*t//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        s = 1\n",
    "        a = 1\n",
    "        b = 1\n",
    "        ans = 0\n",
    "        for i in range(1,n+1):\n",
    "            if s > 7:\n",
    "                s = 1\n",
    "                a = a +1\n",
    "                b = a\n",
    "            ans += b\n",
    "            b +=1\n",
    "            s +=1\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        # 计算完整的星期数\n",
    "        full_weeks = n // 7\n",
    "        # 计算最后一个星期的天数\n",
    "        remaining_days = n % 7\n",
    "\n",
    "        # 第一周的总存款是 (1+7)*7/2 = 28\n",
    "        # 对于接下来的每一周，每天的存款都会比前一周的同一天多1块钱。\n",
    "        \n",
    "        # 计算完整星期的总存款\n",
    "        total = 28 * full_weeks + 7 * (full_weeks * (full_weeks - 1)) // 2\n",
    "\n",
    "        # 计算最后一个不完整星期的存款\n",
    "        for i in range(1, remaining_days + 1):\n",
    "            total += full_weeks + i\n",
    "\n",
    "        return total\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        ans=0\n",
    "        x=0\n",
    "        while n>7:\n",
    "            n-=7\n",
    "            ans+=28\n",
    "            ans+=x*7\n",
    "            x+=1\n",
    "        return ans+x*n+((n+1)*n)//2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def totalMoney(self, n: int) -> int:\n",
    "        base = 1\n",
    "        result = 0\n",
    "        tmp = 0\n",
    "        for i in range(1, n+1):\n",
    "            if i % 7 == 1:\n",
    "                tmp = base\n",
    "                base += 1\n",
    "            result += tmp\n",
    "            tmp += 1\n",
    "        return result\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
